﻿using DLL.NET.ComPort;

using DLL.NET70.Infrastructure.Prism;
using DLL.NET70.Infrastructure.Prism.MVVM;
using DLL.Standard.Infrastructure.Log;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;

namespace Module.NetCore.Commonly.ViewModels
{
    public class PUC_OpenComViewModel : CMVVMBase, IRegionMemberLifetime
    {
        public bool KeepAlive
        {
            get
            {
                //return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
                return true;//这个的作用是，只要离开当前切面，就注销掉当前页面
            }
        }

        private SerialSetting settings = new SerialSetting();
        private ISerialPortManager SerialPortManager;

        public PUC_OpenComViewModel(IRegionManager regionManager,
            IEventAggregator ea,
            IApplicationCommands applicationCommands,
            IContainerExtension container,
            ISerialPortManager serialPortManager
            ) : base(regionManager, ea, applicationCommands)
        {
            SerialPortManager = serialPortManager;
            try
            {
                cb_ComNameSelectedChangedCommand = new DelegateCommand(CB_ComNameSelectedChanged);
                cb_BaudRateSelectedChangedCommand = new DelegateCommand(cb_BaudRateSelectedChanged);
            }
            catch (Exception e)
            {
                LogHelper.Warn("PUC_OpenComViewModel:" + e.Message);
            }
        }

        #region windows load

        public override void Loaded()
        {
            #region 读取COM口参数文件

            //读取 舵机类型文件，根据同的舵机，读取不同的参数

            #endregion 读取COM口参数文件

            InitializeCom();
            //SerialPortManager.ReadComParam();
            ComName = settings.cSerialEntity.PortName;
            BaudRate = settings.cSerialEntity.BaudRate;
            base.Loaded();
        }

        public override void UnLoaded()
        {
            SerialPortManager.CloseCom();
            base.UnLoaded();
        }

        #endregion windows load

        #region Com name   Process

        //CComBase comBase ;
        private void InitializeCom()
        {
            try
            {
                #region com command

                //串口名称
                settings = SerialPortManager.CurrentSerialSettings;
                ComNames = settings.PortNameCollection;
                BaudRates = settings.BaudRateCollection;

                #endregion com command
            }
            catch (Exception e)
            {
                LogHelper.Warn("PUC_OpenComViewModel:" + e.Message);
            }
        }

        private string _comName = "COM1";

        public string ComName
        {
            get { return _comName; }
            set { SetProperty(ref _comName, value); }
        }

        //ComNames
        private string[] _comNames;

        public string[] ComNames
        {
            get { return _comNames; }
            set { SetProperty(ref _comNames, value); }
        }

        private object _cb_ComNameSelectItem;

        public object cb_ComNameSelectItem
        {
            get { return _cb_ComNameSelectItem; }
            set { SetProperty(ref _cb_ComNameSelectItem, value); }
        }

        public int _BaudRate = 9600;

        public int BaudRate
        {
            get { return _BaudRate; }
            set { SetProperty(ref _BaudRate, value); }
        }

        private BindingList<int> _baudRates = new BindingList<int>();

        public BindingList<int> BaudRates
        {
            get { return _baudRates; }
            set { SetProperty(ref _baudRates, value); }
        }

        private object _cb_BaudRateSelectItem;

        public object cb_BaudRateSelectItem
        {
            get { return _cb_BaudRateSelectItem; }
            set { SetProperty(ref _cb_BaudRateSelectItem, value); }
        }

        public DelegateCommand cb_ComNameSelectedChangedCommand { get; private set; }

        private void CB_ComNameSelectedChanged()
        {
            if (cb_ComNameSelectItem != null)
            {
                ComName = cb_ComNameSelectItem.ToString();
                settings.cSerialEntity.PortName = ComName;
            }
        }

        public DelegateCommand cb_BaudRateSelectedChangedCommand { get; private set; }

        private void cb_BaudRateSelectedChanged()
        {
            if (cb_BaudRateSelectItem != null)
            {
                BaudRate = Convert.ToInt32(cb_BaudRateSelectItem.ToString());
                settings.cSerialEntity.BaudRate = BaudRate;
            }
        }

        private string _connectedStateBrush = "Gray";

        public string ConnectedStateBrush
        {
            get { return _connectedStateBrush; }
            set { SetProperty(ref _connectedStateBrush, value); }
        }

        private bool _isEnabled = false;

        public bool ComIsEnabled
        {
            get { return _isEnabled; }
            set
            {
                SetProperty(ref _isEnabled, value);
            }
        }

        private bool _OpenComEnabled = true;

        public bool OpenComEnabled
        {
            get { return _OpenComEnabled; }
            set
            {
                SetProperty(ref _OpenComEnabled, value);
            }
        }

        public DelegateCommand OpenComCommand
        { get { return new DelegateCommand(OpenCom).ObservesCanExecute(() => OpenComEnabled); } }

        public DelegateCommand CloseComCommand
        { get { return new DelegateCommand(CloseCom).ObservesCanExecute(() => ComIsEnabled); } }

        private void OpenCom()
        {
            SerialPortManager.CurrentSerialSettings = settings;
            bool ret = false;// SerialPortManager.OpenCom();
            if (false == ret)
            {
                LogMessage("串口打开失败！");
                return;
            }
            if (SerialPortManager.IsOpen == true)
            {
                ConnectedStateBrush = "Green";
                ComIsEnabled = true;
                OpenComEnabled = false;
            }
            else
            {
                ConnectedStateBrush = "Gray";
                OpenComEnabled = true;
                ComIsEnabled = false;
            }
            LogMessage("串口打开成功！");
        }

        private void CloseCom()
        {
            try
            {
                if (SerialPortManager.IsOpen == true)
                {
                    SerialPortManager.CloseCom();
                }
                if (SerialPortManager.IsOpen == true)
                {
                    ConnectedStateBrush = "Green";
                    ComIsEnabled = true;
                }
                else
                {
                    ConnectedStateBrush = "Gray";
                    ComIsEnabled = false;
                }
            }
            catch (Exception e)
            {
                LogMessage("关闭串口出现异常：" + e.Message);
            }
            OpenComEnabled = true;
            ComIsEnabled = false;
        }

        public DelegateCommand SaveComCommand
        {
            get { return new DelegateCommand(SaveCom); }//.ObservesCanExecute(() => IsEnabled);
        }

        private void SaveCom()
        {
            if ((ComName.Length > 0) && (BaudRate > 0))
            {
                settings.cSerialEntity.BaudRate = BaudRate;
                settings.cSerialEntity.PortName = ComName;
                SerialPortManager.CurrentSerialSettings = settings;
                // SerialPortManager.SaveComParam();
                LogMessage("串口参数保存成功！");
            }
            else
            {
                LogMessage("串口参数保存失败！");
            }
        }

        #endregion Com name   Process
    }
}